package com.xhj.xmly.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xhj.xmly.R;
import com.xhj.xmly.activity.MainActivity;
import com.xhj.xmly.bean.RecommendAlbums;
import com.xhj.xmly.fragment.RecommendContentFragment;

import java.util.List;

/**
 * Created by Administrator on 2016/12/3.
 */

public class DiscoverRecyclerAdapter extends RecyclerView.Adapter<DiscoverRecyclerAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<RecommendAlbums> list;
    private FragmentManager manager;
    private int position;
    public DiscoverRecyclerAdapter(Context context, List<RecommendAlbums> list, FragmentManager manager) {
        this.context = context;
        this.list = list;
        this.manager = manager;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.discover_recommend_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        this.position = position;
        holder.title.setText(list.get(position).getTitle());
        holder.name.setText(list.get(position).getList().get(0).getTitle());
        holder.name2.setText(list.get(position).getList().get(1).getTitle());
        holder.name3.setText(list.get(position).getList().get(2).getTitle());
        holder.trackTitle.setText(list.get(position).getList().get(0).getTrackTitle());
        holder.trackTitle2.setText(list.get(position).getList().get(1).getTrackTitle());
        holder.trackTitle3.setText(list.get(position).getList().get(2).getTrackTitle());
        Picasso.with(context).load(list.get(position).getList().get(0).getCoverLarge()).into(holder.image);
        Picasso.with(context).load(list.get(position).getList().get(1).getCoverLarge()).into(holder.image2);
        Picasso.with(context).load(list.get(position).getList().get(2).getCoverLarge()).into(holder.image3);

        holder.image.setOnClickListener(this);
        holder.image2.setOnClickListener(this);
        holder.image3.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        String id  = "";
        switch (v.getId()) {
            case R.id.image:
                id = list.get(position).getList().get(0).getId();
                break;
            case R.id.image2:
                id = list.get(position).getList().get(1).getId();
                break;
            case R.id.image3:
                id = list.get(position).getList().get(2).getId();
                break;
        }
        RecommendContentFragment recommendContentFragment = RecommendContentFragment.newInstance(id);
        manager.beginTransaction().replace(R.id.drawer_container,recommendContentFragment).commit();
        MainActivity activity = (MainActivity) context;
        FrameLayout fram = activity.getFram();
        activity.getDrawerLayout().openDrawer(fram);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView title,trackTitle,name,trackTitle2,name2,trackTitle3,name3;
        private ImageView image,image2,image3;
        public ViewHolder(View itemView) {

            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tit);
            trackTitle = (TextView) itemView.findViewById(R.id.trackTitle);
            trackTitle2 = (TextView) itemView.findViewById(R.id.trackTitle2);
            trackTitle3 = (TextView) itemView.findViewById(R.id.trackTitle3);
            name = (TextView) itemView.findViewById(R.id.name);
            name2= (TextView) itemView.findViewById(R.id.name2);
            name3 = (TextView) itemView.findViewById(R.id.name3);
            image = (ImageView) itemView.findViewById(R.id.image);
            image2 = (ImageView) itemView.findViewById(R.id.image2);
            image3 = (ImageView) itemView.findViewById(R.id.image3);
        }
    }

}
